package hd.util.file;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import org.apache.http.HttpStatus;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * 文件下载
 * <p/>
 * Created by apple on 14/11/4.
 */
public class FileDownload {

    private static OnFileDownload fileDownload;

    /**
     * 获取文件信息
     *
     * @param url url
     * @return fileInfo
     */
    public static FileInfo loadFileInfo(String url) {
        try {
            URL absUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) absUrl.openConnection();
            HttpURLConnection.setFollowRedirects(true);
            int code = conn.getResponseCode();//获取响应code
            if (code == HttpStatus.SC_OK) {
                FileInfo info = new FileInfo();
                info.setHeader(conn.getHeaderFields());
                info.setInputStream(conn.getInputStream());
                return info;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下载文件
     *
     * @param fileInfo 文件信息
     * @param filePath 文件路径
     * @param fileName 文件名
     * @param download 文件下载
     */
    public static void fileDownload(final FileInfo fileInfo, final String filePath, final String fileName, OnFileDownload download) {
        fileDownload = download;
        new Thread() {
            @Override
            public void run() {
                FileUtil.makeRootDirectory(filePath);
                File file = new File(filePath, fileName);

                FileOutputStream os;
                long loadSize = 0;
                byte[] bytes = new byte[1024];
                InputStream is = fileInfo.getInputStream();
                try {
                    os = new FileOutputStream(file);
                    int len;
                    while ((len = is.read(bytes)) != -1) {
                        os.write(bytes, 0, len);
                        loadSize += len;
                        Message msg = new Message();
                        msg.obj = loadSize;
                        msg.what = 0;
                        handler.sendMessage(msg);
                    }
                    is.close();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);
                }
            }
        }.start();
    }

    private static Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    fileDownload.onDownloading((Long) msg.obj);
                    break;
                case 1:
                    fileDownload.onDownloading(-1);//下载失败
                    break;
            }
        }
    };

}